package cn.neusoft.service.impl;

import cn.neusoft.mapper.UserMapper;
import cn.neusoft.pojo.User;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.UserQuery;
import cn.neusoft.service.IUserService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.java2d.pipe.SpanClipRenderer;

import java.util.List;

@Service

public class UserService implements IUserService {
	@Autowired
	private UserMapper userMapper;
	@Transactional
	public ResultBean add(User user) throws Exception {
		if (user==null ||
				StringUtils.isEmpty(user.getUsername())){
			return ResultBean.ERROR("请求参数为空值");
		}

		int add = userMapper.add(user);

		return add>0?ResultBean.SUCCESS(user.getUserId()):ResultBean.ERROR("添加用户信息失败");
	}
	@Transactional
	public ResultBean update(User user) throws Exception {
		if(user==null ||
			user.getUserId()==null || user.getUserId()==0 ||
			StringUtils.isEmpty(user.getUsername())) {
			return ResultBean.ERROR("请求参数为空");
		}
		int count=userMapper.update(user);
		return count>0?ResultBean.SUCCESS():ResultBean.ERROR("update is failed");
	}

	//这里的删除并不是真正的删除，而是只是让删除标记变为零
	@Transactional
	public ResultBean delete(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		//先查询出这个对象
		User user = userMapper.getById(id);
		if(user==null) {
			return ResultBean.ERROR("没有此用户");
		}
		//修改完之后还得进行同步到数据库
		user.setDelTag(0);
		userMapper.update(user);
		return ResultBean.SUCCESS();
	}
	@Transactional(readOnly = true)
	public ResultBean getById(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		User user = userMapper.getById(id);
		return user!=null?ResultBean.SUCCESS(user):ResultBean.ERROR("getById is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getAll() throws Exception {
		List<User>  list=userMapper.getAll();
		return list!=null?ResultBean.SUCCESS(list):ResultBean.ERROR("getAll is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
		//这里在传子类的查询的时候，接收的是父类进行接收 ，发生向上转型
		//如果不进行设置分页的条件会按照默认的进行分页
		PageBean<User> pageBean=new PageBean<User>();
		if(baseQuery==null) {
			baseQuery=new UserQuery();
		}
		int count=userMapper.getCount(baseQuery);
		pageBean.setTotalRows(count);
		//这里为什么不需要进行计算总页数和总记录数
		List<User> list= userMapper.getByPage(baseQuery);
		pageBean.setData(list);
		return ResultBean.SUCCESS(pageBean);
	}
	@Transactional
	@Override
	public ResultBean batchDel(List<Integer> ids) throws Exception {
		if(ids==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		for (Integer id: ids) {
			int delete = userMapper.delete(id);
			if(delete==0) {
				System.out.println("删除用户失败");
			}
		}
		return ResultBean.SUCCESS();
	}

	@Override
	public ResultBean login(String username, String password) {
		if(username==null || StringUtils.isEmpty(username) ||
				password==null || StringUtils.isEmpty(password)) {
			return ResultBean.ERROR("请求参数为空");
		}
		User user=userMapper.getByUsername(username);
		if(user==null) {
			return ResultBean.ERROR("用户名不存在");
		}
		user.setPassword(null);
		//将这个用户的信息响应给前端
		return ResultBean.SUCCESS(user);
	}
}
